Software developers must ensure that the software, i.e., the written software code, functions properly. In order to ensure proper code function, the written software code must pass known parameters and not “break” code, i.e., the written code must produce expected results. Test cases are a tool used by software developers to help ensure that software code functions as expected.
Test cases are written for methods or functions in the software code. These test cases are typically written as the software code is written. When writing test cases the test cases may require test environment objects. These objects can be used directly by the test cases of passed in as parameters.
JUnit is an open source test framework that allows developer to write and run repeatable tests on software. One major issue in writing JUnit test cases for complex software is the time required to write code to setup the test environment. JUnit test cases often require the creation of complex objects arguments to pass to the method under test or the creation of complex objects in the setup method so that the objects can be used by the method under test. The code to create these environmental objects can sometimes be longer than the implementation of the method being tested. In addition, if these environmental objects involve third party objects their creation may be even more difficult or unknown.
The current solution to this problem is to create mock objects. Mock objects provide a simulated implementation of the real objects needed for JUnit testing. In this process mock objects are used instead of passing real objects to the test methods. While mock objects can be effective for many test cases they also require considerable time to write. In some cases, the source code under test must be modified to accommodate the mock objects during test. Furthermore, the number of lines of code required to be written for mock objects may still exceed the number of lines of code being tested.